import { IConfig } from 'umi';
import routes from './routes';
import slash from 'slash2';
import dayjs from 'dayjs';

const pjson = require('../package.json');
//判断只有在生产模式才开启
const IS_PROD = ['production', 'prod'].includes(process.env.NODE_ENV);

export default {
  routes,
  outputPath: './html',
  hash: true,
  targets: {
    ie: 10,
  },
  theme: {
    'primary-color': '#1890FF',
  },
  lessLoader: {
    javascriptEnabled: true,
  },
  cssLoader: {
    modules: {
      getLocalIdent: (
        context: {
          resourcePath: string;
        },
        _: string,
        localName: string,
      ) => {
        if (
          context.resourcePath.includes('node_modules') ||
          context.resourcePath.includes('ant.design.pro.less') ||
          context.resourcePath.includes('global.less')
        ) {
          return localName;
        }
        const match = context.resourcePath.match(/src(.*)/);
        if (match && match[1]) {
          const antdProPath = match[1].replace('.less', '');
          const arr = slash(antdProPath)
            .split('/')
            .map((a: string) => a.replace(/([A-Z])/g, '-$1'))
            .map((a: string) => a.toLowerCase());
          return `smartbox${arr.join('-')}-${localName}`.replace(/--/g, '-');
        }
        return localName;
      },
    },
  },
  manifest: {
    basePath: '/',
  },
  define: {
    'process.env.version': pjson.version,
    'process.env.time': dayjs().format('YYYY-MM-DD HH:mm:ss'),
  },
  externals: {},
  // 替换moment.js/dayjs
  ignoreMomentLocale: true,
  chainWebpack(config) {
    // antd moment -> dayjs
    config.plugin('moment2dayjs').use('antd-dayjs-webpack-plugin', []);
  },
  dva: {
    hmr: true,
  },
  dynamicImport: {
    loading: '@/components/PageLoading/index',
  },
  proxy: {
    // '/raven/v1': {
    //   target: 'http://192.168.2.56',
    //   changeOrigin: true,
    // },
    '/stats/v1': {
      target: 'http://192.168.2.123:8008',
      changeOrigin: true,
    },
  },
  extraBabelPlugins: [IS_PROD ? 'transform-remove-console' : ''],
  qiankun: {
    master: {},
  },
} as IConfig;
